<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<form class="layui-form">
    <fieldset class="layui-elem-field">
        <legend>详细信息</legend>
        <div class="layui-field-box">

        <#--买赠编号 隐藏 传递的时候是空值，在后台用账号生成器自动生成-->
            <input type="hidden" name="id" value="params.id">

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">买赠编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="gNo" value="${params.gNo!''}" readonly lay-verify="require"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">买赠名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="gName" value="${params.gName!''}" lay-verify="require"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">满赠促销方式</label>
                <div class="layui-input-inline">
                    <select lay-filter="conNo" name="conNo" lay-verify="required">
                        <option value="${params.conNo!''}">${params.conName!''}</option>
                    <#if conList ? exists>
                        <#list conList as item>
                            <option value="${item.conNo!''}">${item.conName!''}</option>
                        </#list>
                    <#else >
                    </#if>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">商品类别</label>
                    <div class="layui-input-inline">
                        <select id="pPrdNo" class="pPrdNo" lay-filter="pPrdNo" name="pPrdNo">
                        <option value="${params.pPrdNo!''}">${params.prdInfo[0].prd_name!''}</option>
                        <#list map as info>
                            <option value="${info.prdNo!''}" abc="${info.prdName!''}">${info.prdName!''}</option>
                        </#list>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">赠品类别</label>
                    <div class="layui-input-inline">
                        <select id="gPrdNo" name="gPrdNo">
                            <option value="${params.prdInfo[0].prd_dtl_id!''}">${params.prdInfo[0].prd_dtl_name!''}</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="updateSLGF">确认</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </div>
    </fieldset>
</form>
<script type="text/javascript" src="/static/layui/layui.js"></script>
<script>
    //Demo
    // 待学生自主完成
    layui.use(['form', 'jquery', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form();
        var layer = layui.layer;


//        商品分类和赠品的联动
        form.on("select(pPrdNo)", function (data) {
//            自定义的select标签的属性值
            var abc = $("#pPrdNo option:selected").attr("abc");
            console.log(abc);
            console.log(data.value);
            $.ajax({
                url: "/slgf/getPrdDetailInfo?prdName=" + abc,
                type: "POST",
                data: data.field,
                success: function (data) {
                    console.log(data.length);
                    $("#gPrdNo").find('option').remove();
                    for (var i = 0; i < data.length; i++) {
                        $("#gPrdNo").append("<option value='" + data[i].prd_dtl_id + "'>" + data[i].prd_dtl_name + "</option>");
                    }
                    // 重新渲染页面
                    form.render();
                }
            });

        });


        //监听修改提交
        form.on('submit(updateSLGF)', function (params) {
            var data = $("form").serializeArray();
            $.ajax({
                type: "POST",
                url: "/slgf/updateSLGFInfo",  //后台程序地址
                data: data,  //需要post的数据
                success: function (data) {           //后台程序返回的标签，比如我喜欢使用1和0 表示成功或者失败
                    if (data.result == 'success') {   //如果成功了, 则关闭当前layer
                        layer.msg('成功', {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function () {//
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    }
                    else if (data.result == 'defeat') {
                        layer.msg('修改失败')
                    }
                }
            });
            return false;//return false 表示不通过页面刷新方式提交表单
        });

        // 重新渲染页面
        form.render();

    });
</script>
</body>
</html>